using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using UltimateDefend.Properties;
using System.IO;
using System.Reflection;

namespace UltimateDefend
{
    public abstract class IMap
    {
        #region fields

        protected ImageAttributes m_imgAtt;
        protected Bitmap m_bmpMap;

        //luu danh sach cac rect co xay dung tree
        protected List<Rectangle> m_listTree;

        protected List<ITower> m_pListTower;        //luu danh sach cac tower hien co trong ban do

        protected int m_Level;               //luu level
        #endregion fields

        #region properties

        public List<ITower> ListTower
        {
            get { return this.m_pListTower; }
            set { this.m_pListTower = value; }
        }

        public Bitmap BitmapMap
        {
            get { return this.m_bmpMap; }
        }

        #endregion properties

        /// <summary>
        /// kiem tra xem 1 dien tich rect cho truoc co giao voi khu vuc co tree hay khong
        /// </summary>
        /// <param name="rect">dien tich can kiem tra</param>
        /// <returns>
        /// true: co giao voi khu vuc tree
        /// false: khong giao voi khu vuc tree
        /// </returns>
        public bool IsInListPointOfTree(Rectangle rect)
        {
            int i;
            for (i = 0; i < this.m_listTree.Count; i++)
            {
                if (rect.IntersectsWith(this.m_listTree[i]))
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        /// kiem tra xem 1 dien tich rect cho truoc co giao voi khu vuc da xay dung Tower hay khong
        /// </summary>
        /// <param name="rect">dien tich can kiem tra</param>
        /// <returns>
        /// true: co giao voi khu vuc da xay dung tower
        /// false: khong giao voi khu vuc da xay dung tower
        /// </returns>
        public bool IsInListTower(Rectangle rect)
        {
            int i;

            for (i = 0; i < this.m_pListTower.Count; i++)
            {
                if (rect.IntersectsWith(this.m_pListTower[i].GetRect()))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// kiem tra xem 1 diem cho truoc co nam trong khu vuc da xay dung Tower hay khong
        /// </summary>
        /// <param name="p">diem can kiem tra</param>
        /// <returns>
        /// index cua tower ma diem p nam ben trong
        /// -1 neu diem p nam ben ngoai tat ca cac tower
        /// </returns>
        public int IsInListTower(Point p)
        {
            int i;
            Rectangle rectTmp = new Rectangle();
            for (i = 0; i < this.m_pListTower.Count; i++)
            {
                rectTmp = this.m_pListTower[i].GetRect();
                if (rectTmp.Contains(p))
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// ve lai ban do khi co su thay doi tren ban do nhu: sell tower, bulld tower, upgrade tower
        /// </summary>
        /// <param name="bmpCurr">ban do luc dau khi chua co tower nao</param>
        public void RefreshMap(Bitmap bmpCurr)
        {
            //ve lai danh sach cac tower con lai tren ban do
            Graphics g = Graphics.FromImage(bmpCurr);
            int i;
            for (i = 0; i < this.m_pListTower.Count; i++)
            {
                if (this.m_pListTower[i].IsSelect)
                {
                    Pen pen = new Pen(Color.Blue);
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                    pen.Width = 3;

                    g.DrawEllipse(pen, this.m_pListTower[i].Position.X - this.m_pListTower[i].Width / 2, this.m_pListTower[i].Position.Y + this.m_pListTower[i].Height / 4, this.m_pListTower[i].Width, this.m_pListTower[i].Height / 3);

                    pen.Dispose();
                }
                this.m_pListTower[i].DrawTower(bmpCurr, false, Color.Red);
            }
            g.Dispose();
        }
        /// <summary>
        /// ham de cho nguoi dung chon tower
        /// </summary>
        /// <param name="index">index cua tower muon chon</param>
        public void SeletedTower(int index)
        {
            int i;
            for (i = 0; i < this.m_pListTower.Count; i++)
            {
                if (i == index)
                {
                    this.m_pListTower[i].IsSelect = true;
                }
                else
                {
                    this.m_pListTower[i].IsSelect = false;
                }
            }
        }


        /// <summary>
        /// ham de ve hinh nen
        /// </summary>
        /// <param name="bitmap">bitmap co kich thuoc bang man hinh, do nguoi dung new</param>
        /// <returns>
        /// 
        /// </returns>
        /// dung co xoa gi het nha ku!
        public void LoadMap(Bitmap bitmap)
        {
            //lay thu muc goc
            string rootStr = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

            m_listTree = new List<Rectangle>();

            //load len picture cua map
            this.m_bmpMap = new Bitmap(rootStr + @"\Map\a" + ".JPG");
            Graphics g = Graphics.FromImage(bitmap);
            g.DrawImage(this.m_bmpMap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, m_bmpMap.Width, m_bmpMap.Height), GraphicsUnit.Pixel);
            g.Dispose();

            /************************************/
            //doc vi tri cua cac tree trong map (chuyen ve cai toa do chuan gi cua may nua!)
            StreamReader sr = new StreamReader(rootStr + @"\Map\a" + ".txt");
            //bo hang dau tien
            sr.ReadLine();
            char[] arrChar = { ' ' };
            while (!sr.EndOfStream)
            {
                string tmp = sr.ReadLine();
                string[] arrStr = tmp.Split(arrChar);
                /******cac vi tri can chuyen**********/
                int x = int.Parse(arrStr[0]);
                int y = int.Parse(arrStr[1]);
                int width = int.Parse(arrStr[2]);
                int height = int.Parse(arrStr[3]);
                /**************************************/
                Rectangle rect = new Rectangle(x, y, width, height);
                m_listTree.Add(rect);
            }

            sr.Close();
        }


        /// <summary>
        /// ham dung de add 1 tower moi xay dung vao trong listTower
        /// </summary>
        /// <param name="tower">tower moi xay dung</param>
        public void AddTower(ITower tower)
        {
            this.m_pListTower.Add(tower);
        }

        /// <summary>
        /// tim kiem tower
        /// </summary>
        /// <param name="tower">tower can tim</param>
        /// <returns>index cua tower tim thay, hoac tra ve -1 neu khong tim thay</returns>
        protected int FindTower(ITower tower)
        {
            int i;
            for (i = 0; i < this.m_pListTower.Count; i++)
            {
                if (this.m_pListTower[i] == tower)
                {
                    return i;
                }
            }
            return -1;
        }
        /// <summary>
        /// ham dung de remove 1 tower ra khoi listTower, su dung khi nguoi dung sell tower
        /// </summary>
        /// <param name="tower">tower bi sell</param>
        public void RemoveTower(ITower tower)
        {
            //this.m_pListTower.Find(Predicate<ITower> tower).IsDestroy = true;
            int index = this.FindTower(tower);

            this.m_pListTower[index].IsDestroy = true;

            this.m_pListTower.Remove(tower);
        }

        /// <summary>
        /// ham dung de remove 1 tower ra khoi listTower, su dung khi nguoi dung sell tower
        /// </summary>
        /// <param name="index">index cua tower bi sell</param>
        public void RemoveTower(int index)
        {
            this.m_pListTower.RemoveAt(index);
        }
    }

    public class MapLevel1 : IMap
    {
        public MapLevel1()
        {
            this.m_pListTower = new List<ITower>();

            this.m_Level = 1;
        }
    }

    public class MapLevel2 : IMap
    {
        public void GenerateListPoint()
        {

        }
    }
}
